package com.ruoyi.project.demo.course.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.demo.course.domain.Course;
import com.ruoyi.project.demo.course.service.ICourseService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.demo.order.domain.CourseOrder;
import com.ruoyi.project.demo.order.service.ICourseOrderService;
import com.ruoyi.project.system.user.service.IUserService;

/**
 * 知识管理Controller
 *
 * @author demo
 * @date 2020-04-19
 */
@Controller
@RequestMapping("/demo/course")
public class CourseController extends BaseController {
    private String prefix = "demo/course";

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("demo:course:view")
    @GetMapping()
    public String course() {
        return prefix + "/course";
    }

    /**
     * 查询知识管理列表
     */
    @RequiresPermissions("demo:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Course course) {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出知识管理列表
     */
    @RequiresPermissions("demo:course:export")
    @Log(title = "知识管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Course course) {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 新增知识管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增知识管理
     */
    @GetMapping("/getUser/{id}")
    public String getUser(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/getUser";
    }

    /**
     * 新增知识管理
     */
    @PostMapping("/getUserList")
    @ResponseBody
    public TableDataInfo getUserList(Long id) {
        startPage();
        CourseOrder order = new CourseOrder();
        order.setArrangeId(id);
        List<CourseOrder> courseOrders = courseOrderService.selectCourseOrderList(order);
        for (int i = 0; i < courseOrders.size(); i++) {
            try {
                courseOrders.get(i).setUserName(userService.selectUserById(courseOrders.get(i).getUserId()).getUserName());
            } catch (Exception e) {

            }
        }
        return getDataTable(courseOrders);

    }

    /**
     * 新增保存知识管理
     */
    @RequiresPermissions("demo:course:add")
    @Log(title = "知识管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Course course) {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改知识管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Course course = courseService.selectCourseById(id);
        mmap.put("course", course);
        return prefix + "/edit";
    }

    /**
     * 修改保存知识管理
     */
    @RequiresPermissions("demo:course:edit")
    @Log(title = "知识管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Course course) {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除知识管理
     */
    @RequiresPermissions("demo:course:remove")
    @Log(title = "知识管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}
